package br.com.sistemasaude.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.sistemasaude.bean.Atendimento;
import br.com.sistemasaude.bean.Especialidade;
import br.com.sistemasaude.bean.Material;
import br.com.sistemasaude.bean.RelMatAtend;
import br.com.sistemasaude.util.JpaUtil;

public class RelMatAtendDAOImpl implements RelMatAtendDAO{
	
	
	private final GenericDAO<RelMatAtend> dao;	
	EntityManager em = new JpaUtil().getEntityManager();
	
	public RelMatAtendDAOImpl() {
		dao = new GenericDAO<RelMatAtend>(em, RelMatAtend.class);
		}
	
	
	
public void salvar(RelMatAtend relMatAtend) {
		
		try {

			em.getTransaction().begin();
			dao.salvar(relMatAtend);
			em.getTransaction().commit();

		} catch (Exception e) {
			
			em.getTransaction().rollback();
			e.printStackTrace();
			
		} finally {
			em.close();
		}
		
	}

public List<Material> retornaMateriais(Atendimento atendimento){

	 List<Material> materiais = new ArrayList<Material>();
	Query query = em.createQuery("select r.tbMaterial from RelMatAtend r where r.tbAtendimento = :pAtendimento ");
	query.setParameter("pAtendimento", atendimento);
	materiais = query.getResultList();
	return materiais;
	
}

}
